<?php
/*
 * @Description    : 腾讯云验证码
 * @Version        : 1.0.0
 * @Author         : QianLong
 * @Date           : 2020-10-29 15:53:35
 * @LastEditors    : QianLong
 * @LastEditTime   : 2023-03-02 18:46:28
 */

namespace app\common\service;

use app\common\service\tencent\Base;
use app\lib\exception\ApiException;

class TencentCaptcha
{
    protected $config;
    public function __construct($config)
    {
        $this->config = $config;
    }
    public function check($params)
    {
        // 进行在线验证
        $host   = 'captcha.tencentcloudapi.com';
        $params = [
            'CaptchaType'  => 9,
            'Ticket'       => $params['token'],
            'UserIp'       => request()->ip(0, true),
            'Randstr'      => $params['authenticate'],
            'CaptchaAppId' => (int)$this->config['tenc_capt_app_id'],
            'AppSecretKey' => $this->config['tenc_capt_secret_key']
        ];

        $result = (new Base($this->config))->curl_post($host, $params,'DescribeCaptchaResult','captcha','2019-07-22');
        $result = json_decode($result, true);
        if (isset($result['Response']['Error']['Code'])) {
            throw new ApiException($result['Response']['Error']['Code'] . '; [Message：' . $result['Response']['Error']['Message'] . ']');
        }
        if (isset($result['Response']['CaptchaCode']) && $result['Response']['CaptchaCode'] != 1) {
            throw new ApiException($result['Response']['CaptchaMsg']);
        }
        return true;
    }
}
